import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'

import Welcome from '../components/Welcome'
import Users from '../components/user/users'
import Rights from '../components/r/rights'
import Roles from '../components/r/roles'
import Goods from '../components/shop/goods'
import Params from '../components/shop/params'
import Categories from '../components/shop/categories'
import Orders from '../components/orders/orders'
import Reports from '../components/reports/reports'
Vue.use(VueRouter)


//element ui修复bug
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  { path: '/', redirect: './login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/roles',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/goods', component: Goods },
      { path: '/params', component: Params },
      { path: '/categories', component: Categories },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Reports },
    ]
  },
]

const router = new VueRouter({
  routes
})

//在暴露之前挂载路由守卫
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    return next()
  }

  //获取token
  const token = window.sessionStorage.getItem('token')

  if (!token) {
    return next('/login')
  }

  next();

})

export default router
